// pages/brokerSwoole/brokerSwoole.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    webSocket:'',
    uid:'',
    brokerId:'',
    message:'',
    list:[],
    value:'',
    userImage:'',
    brokerImge:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var uid=options.uid
        this.setData({
          uid
        })
    wx.request({
      url: 'http://www.house.com/index.php/api/brockerInfo',
      header:{
        token:wx.getStorageSync('token')
      },
      data:{
        uid
      },
      method:"POST",
      success:res=>{
        if(res.data.code==200){
           this.setData({
            brokerId:res.data.data.brokerId,
            brokerImage:res.data.data.brokerImage,
            userImage:res.data.data.userImage
           })
           this.sendInfo('open')
        }
      }
    })

    var webSocket=wx.connectSocket({
      url: 'ws://81.68.163.7:9505',
    })
    this.setData({
      webSocket
    })
   
     /**
     * 处理信息
     */
    wx.onSocketMessage((result) => {
      let list = this.data.list;
      var data=JSON.parse(result.data)
      let newList = list.concat(data)
      this.setData({
        list:newList
      })
      
    })
  },


  /**
   * 发送信息
   */
  send(){
    this.sendInfo('send',this.data.message)
  },

  /**
   * 方法封装
   */
  sendInfo(type='open',message=''){
    var message={
      uid:wx.getStorageSync('broker'),
      tid:this.data.uid,
      data:message,
      type:type
    }
    var Message=JSON.stringify(message)
    wx.sendSocketMessage({
      data: Message,
      success:res=>{
       this.setData({
        value:''
       })
      }
    })
  },

  /**
   * 
   * 发送的信息
   */
  content(e){
    var message= e.detail.value
    this.setData({
      message
    })
  }

   
})